<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\components\helpers\Fun;
use doumiaoduo\ueditor\ImageUploadMultipleBehavior2;
use doumiaoduo\ueditor\ImageUploadMultipleTrait2;
use doumiaoduo\ueditor\VideoUploadMultipleBehavior;
use doumiaoduo\ueditor\VideoUploadMultipleTrait;
use Yii;
use yii\helpers\ArrayHelper;
use yii\validators\Validator;

/**
 * This is the model class for table "{{%lesson_student}}".
 *
 * @property integer $id
 * @property integer $lesson_id
 * @property integer $check_time
 * @property integer $status
 * @property string $lesson_content
 * @property string $lesson_fouce
 * @property integer $lesson_score
 * @property integer $teacher_score
 * @property string $for_teacher
 * @property string $refouce_notice
 * @property integer $refouce_score
 * @property string $refouce_answer
 * @property integer $student_id
 */
class LessonStudent extends \zhtfrontend\components\bases\BaseModel
{
    use ImageUploadMultipleTrait2,VideoUploadMultipleTrait;

    public $created_at;

    public $picture;


    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $validators = $this->getValidators();
        $validators->append(Validator::createValidator('safe', $this, (array) ['videoList'], []));
        $validators->append(Validator::createValidator('safe', $this, (array) ['imageList','altList','defaulturl'], []));
        $this->attachBehavior('VideoUploadMultipleBehavior',['class'=>VideoUploadMultipleBehavior::className()]);
        $this->attachBehavior('ImageUploadMultipleBehavior2',['class'=>ImageUploadMultipleBehavior2::className()]);

    }
    //public $videoList;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%lesson_student}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['lesson_id', 'check_time', 'status', 'lesson_score', 'teacher_score', 'refouce_score', 'student_id','teacher_growth_score','headmaster_growth_score','student_teacher_score'], 'integer'],
            [['lesson_content', 'lesson_fouce', 'for_teacher', 'refouce_notice', 'refouce_answer','videoList'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID编号'),
            'lesson_id' => Yii::t('zhtfrontend', '课次序号'),
            'check_time' => Yii::t('zhtfrontend', '开始上课时间'),
            'status' => Yii::t('zhtfrontend', '对应状态(0非正常;1正常;)'),
            'lesson_content' => Yii::t('zhtfrontend', '本课内容'),
            'lesson_fouce' => Yii::t('zhtfrontend', '练习重点'),
            'lesson_score' => Yii::t('zhtfrontend', '评分'),
            'teacher_score' => Yii::t('zhtfrontend', '对老师评分'),
            'for_teacher' => Yii::t('zhtfrontend', '评分留言'),
            'refouce_notice' => Yii::t('zhtfrontend', '练习提问'),
            'refouce_score' => Yii::t('zhtfrontend', '练习评分'),
            'refouce_answer' => Yii::t('zhtfrontend', '提问回复'),
            'student_id' => Yii::t('zhtfrontend', 'Student ID'),
            'imageList'=>Yii::t('zhtfrontend', '图片'),
            'videoList'=>Yii::t('zhtfrontend', '视频'),
            'created_at'=>Yii::t('zhtfrontend', '时间'),
            'picture'=>Yii::t('zhtfrontend', '图片'),
            'teacher_growth_score'=>Yii::t('zhtfrontend', '学生对教师成长日志满意度'),
            'headmaster_growth_score'=>Yii::t('zhtfrontend', '学生对班主任成长日志满意度'),
            'student_teacher_score'=>Yii::t('zhtfrontend', '学生对教师的评分'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['check_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->check_time);
        };
        $fields['status'] = function () {
            switch ($this->status) {
                case 0:
                    return \Yii::t('zhtfrontend', '未上课');
                case 1:
                    return \Yii::t('zhtfrontend', '已上课');
                case 2:
                    return \Yii::t('zhtfrontend', '教师请假');
                case 3:
                    return \Yii::t('zhtfrontend', '学生请假');
                case 4:
                    return \Yii::t('zhtfrontend', '教师停课');
                case 5:
                    return \Yii::t('zhtfrontend', '代课');
                case 6:
                    return \Yii::t('zhtfrontend', '复课');
            }
        };

        return $fields;
    }


    public function extraFields()
    {
        return [
            'student_name' => function () {
                return isset($this->student)?$this->student->name:'';
            },
            'teach_form' => function () {
                return !empty($this->classLesson->ovo)?($this->classLesson->ovo==0?'一对一':'一对多'):'';
            },
            'lesson' => function () {
                return isset($this->classLesson->lesson)?$this->classLesson->lesson:'';
            },
            'course_name' => function () {
                return isset($this->classLesson)?$this->classLesson->title:'';
            },
            'class_date' => function () {
                return isset($this->classLesson)?\Yii::$app->formatter->asDate($this->classLesson->class_date):'';
            },
            'class_time' => function () {
                return isset($this->classLesson)?Fun::getClassTime($this->classLesson->start_time,$this->classLesson->end_time):'';
            },
            'teacher_name' => function () {
                return $this->teacherName;
            },
//            'operate' => function () {
//                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
//            },
        ];
    }

    public function getStudent()
    {
        return $this->hasOne(Student::className(), ['id' => 'student_id']);
    }

    public function getClassLesson()
    {
        return $this->hasOne(ClassLesson::className(), ['lesson_id' => 'lesson_id']);
    }

    public function getTeacherName()
    {
        $teacher_name='';
        if($this->status==5){
            $array=isset($this->classLesson)?unserialize($this->classLesson->info):[];
            $teacher_name=isset($array['teacher_name'])?$array['teacher_name']:'';
        }else{
            $classManage=isset($this->classLesson->classManage)?$this->classLesson->classManage:null;
            if($classManage!=null){
                if($classManage->is_old==1){
                    $teacher_name=isset($classManage->arrangeMent->teacher)?$classManage->arrangeMent->teacher->name:'';
                }else{
                    $goods_id=isset($classManage->goods)?$classManage->goods->id:'';
                    if(!empty($goods_id)){
                        $goods_attr=GoodsAttr::findOne(['goods_id'=>$goods_id,'obi_type'=>'teacher']);
                        $teacher_name=$goods_attr==null?'':$goods_attr->value;
                    }
                }

            }
        }
        return $teacher_name;
    }
}
